﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using Common;
using Microsoft.Reporting.WinForms;
using System.Drawing.Printing;

namespace 生命体征测试仪
{
    public partial class formReport : formBase
    {
        public formReport(MedicalResultData m)
        {
            InitializeComponent();
            MedicalResult = m;
        }
        MedicalResultData MedicalResult;
        int leftTime;
        private void formReport_Load(object sender, EventArgs e)            
        {
            leftTime = 60;
            DataRow dr = reportDataSet.UserTable.NewRow();
            Bitmap right;
            if(formMain.PType==0)
            {
                dr["Name"] = formMain.curUser.Name;
                dr["Sex"] = formMain.curUser.Sex;
                dr["Nation"] = formMain.curUser.Nation;
                dr["Born"] = formMain.curUser.Born;
                dr["Address"] = formMain.curUser.Address;
                dr["IDCardNo"] = formMain.curUser.ID;
                dr["PhotoFileName"] = formMain.PhotoFileName;
                right = new Bitmap(formMain.PhotoFileName);
            }
            else
            {
                dr["Name"] = "临时人员";
                dr["Sex"] = "";
                dr["Nation"] = "";
                dr["Born"] = "";
                dr["Address"] = "";
                dr["IDCardNo"] = "";
                dr["PhotoFileName"] = System.Windows.Forms.Application.StartupPath + "\\新中新身份证\\XXXX.Jpg";
                right = new Bitmap(System.Windows.Forms.Application.StartupPath + "\\新中新身份证\\XXXX.Jpg");
            }
            byte[] bufferRight = BitmapToBytes(right);
            dr["Photo"] = Convert.ToBase64String(bufferRight);
            reportDataSet.UserTable.Rows.Add(dr);
            if(SysConfig.bHaveShenGao)
            {
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "身高";
                dr[1] = MedicalResult.ShenGao.ToString("F1")+" cm";
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "体重";
                dr[1] = MedicalResult.TiZhong.ToString("F1") + " Kg";
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
            }
            if (SysConfig.bHaveTiWen)
            {
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "体温";
                dr[1] = MedicalResult.ErWen.ToString("F1") + " ℃";
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
            }
            if (SysConfig.bHaveXueYa)
            {
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "高压";
                dr[1] = MedicalResult.GaoYa.ToString() + " mmHg";
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "低压";
                dr[1] = MedicalResult.DiYa.ToString() + " mmHg";
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "心率";
                dr[1] = MedicalResult.XinLv.ToString() + " 次/分";
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
            }
            if (SysConfig.bHaveXinDian)
            {
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "心电";
                dr[1] = ((XinDianAnalyseResult)MedicalResult.XinDianResult).ToString();
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
            }
            if (SysConfig.bHaveXueYang)
            {
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "血氧饱和度";
                dr[1] = MedicalResult.SpO2.ToString() + " %";
                dr[2] = "90-100";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "脉率";
                dr[1] = MedicalResult.PR.ToString() + " bpm";
                dr[2] = "50-120";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "血流灌注指数";
                dr[1] = (((float)MedicalResult.PI)/10).ToString("F1") + " %";
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
            }
            if (SysConfig.bHaveXueTang)
            {
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "餐前血糖";
                dr[1] = MedicalResult.XueTang1.ToString("F1") + " umol//L";
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "餐后血糖";
                dr[1] = MedicalResult.XueTang2.ToString("F1") + " umol//L";
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "尿酸";
                dr[1] = MedicalResult.NiaoSuan.ToString("F1") + " umol//L";
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "胆固醇";
                dr[1] = MedicalResult.DanGuChun.ToString("F1") + " mmol//L";
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
            }
            if (SysConfig.bHaveZhiFangYi)
            {
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "脂肪";
                dr[1] = MedicalResult.ZhiFang.ToString("F1") + " %";
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "BMI";
                dr[1] = MedicalResult.BMI.ToString("F1") + "";
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "新陈代谢";
                dr[1] = MedicalResult.XinChenDaiXie.ToString() + " Kcal";
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "腰围";
                dr[1] = MedicalResult.YaoWei.ToString("F1") + " cm";
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "臀围";
                dr[1] = MedicalResult.TunWei.ToString("F1") + " cm";
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "体质";
                dr[1] = ((ZhiFang_TiZhi)MedicalResult.TiZhi).ToString() + "";
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
                dr = reportDataSet.ReportTable.NewRow();
                dr[0] = "体形";
                dr[1] = ((ZhiFang_TiXing)MedicalResult.TiXing).ToString() + "";
                dr[2] = "";
                dr[3] = "";
                reportDataSet.ReportTable.Rows.Add(dr);
            }
            this.reportViewer1.RefreshReport();
            cbZoom.SelectedIndex = 1;
            cbZoom_SelectedIndexChanged(null, null);
        }
        private byte[] BitmapToBytes(Bitmap Bitmap)
        {
            MemoryStream ms = null;
            try
            {
                ms = new MemoryStream();
                Bitmap.Save(ms, ImageFormat.Jpeg);
                byte[] byteImage = new Byte[ms.Length];
                byteImage = ms.ToArray();
                return byteImage;
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            finally
            {
                ms.Close();
            }
        }
        private void skinButton3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            leftTime = 60;
            Warning[] warnings;
            string[] streamids;
            string mimeType;
            string encoding;
            string extension;

            byte[] bytes = reportViewer1.LocalReport.Render(
               "PDF", null, out mimeType, out encoding, out extension,
               out streamids, out warnings);
            string stmp = "D:/导出pdf报表";
            FileInfo f = new FileInfo(stmp + "\\1.bin");
            // 如果文件所在的文件夹不存在则创建文件夹
            if (!Directory.Exists(f.DirectoryName))
                Directory.CreateDirectory(f.DirectoryName);     

            FileStream fs = new FileStream(stmp+"/"+formMain.curUser.ID+".pdf", FileMode.Create);
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();

            MessageBox.Show("报表已经成功导出！", "Info");
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            leftTime = 60;
            string deviceInfo =
              @"<DeviceInfo>" +
              @"  <OutputFormat>emf</OutputFormat>" +
              @"  <PageWidth>21cm</PageWidth>" +
              @"  <PageHeight>27.9cm</PageHeight>" +
              @"  <MarginTop>0in</MarginTop>" +
              @"  <MarginLeft>0in</MarginLeft>" +
              @"  <MarginRight>0in</MarginRight>" +
              @"  <MarginBottom>0in</MarginBottom>" +
             @"</DeviceInfo>";
            Warning[] warnings;
            m_streams = new List<Stream>();
            //将报表的内容按照deviceInfo指定的格式输出到CreateStream函数提供的Stream中。
            reportViewer1.LocalReport.Render("image", deviceInfo, CreateStream, out warnings);
            foreach (Stream stream in m_streams)
                stream.Position = 0;
            Print();
        }
        //声明一个Stream对象的列表用来保存报表的输出数据
        //LocalReport对象的Render方法会将报表按页输出为多个Stream对象。
        private List<Stream> m_streams;
        //用来提供Stream对象的函数，用于LocalReport对象的Render方法的第三个参数。
        private Stream CreateStream(string name, string fileNameExtension,
            Encoding encoding, string mimeType, bool willSeek)
        {
            //如果需要将报表输出的数据保存为文件，请使用FileStream对象。
            Stream stream = new FileStream(name + "." + fileNameExtension,
          FileMode.Create);
            m_streams.Add(stream);
            return stream;
        }
        //用来记录当前打印到第几页了
        private int m_currentPageIndex;

        private void Print()
        {
            m_currentPageIndex = 0;

            if (m_streams == null || m_streams.Count == 0)
                return;
            //声明PrintDocument对象用于数据的打印
            PrintDocument printDoc = new PrintDocument();
            //指定需要使用的打印机的名称，使用空字符串""来指定默认打印机
            //printDoc.PrinterSettings.PrinterName = "";
            //判断指定的打印机是否可用
            if (!printDoc.PrinterSettings.IsValid)
            {
                MessageBox.Show("Can't find printer");
                return;
            }
            //声明PrintDocument对象的PrintPage事件，具体的打印操作需要在这个事件中处理。
            printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
            //执行打印操作，Print方法将触发PrintPage事件。
            printDoc.Print();
        }
        private void PrintPage(object sender, PrintPageEventArgs ev)
        {
            //Metafile对象用来保存EMF或WMF格式的图形，
            //我们在前面将报表的内容输出为EMF图形格式的数据流。
            if(m_currentPageIndex>0)
                return;
            Metafile pageImage =
         new Metafile(m_streams[m_currentPageIndex]);
            //Image pageImage = Image.FromFile(@"E:\temp\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\Report1.TIF");
            ev.Graphics.DrawImage(pageImage, ev.PageBounds);
            m_currentPageIndex++;
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
        }

        private void cbZoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            leftTime = 60;
            switch (cbZoom.SelectedIndex)
            {
                case 0:
                    reportViewer1.ZoomMode = ZoomMode.FullPage;
                    break;
                case 1:
                    reportViewer1.ZoomMode = ZoomMode.PageWidth;
                    break;
                case 2:
                    reportViewer1.ZoomPercent = 100;
                    reportViewer1.ZoomMode = ZoomMode.Percent;                    
                    break;
                case 3:
                    reportViewer1.ZoomPercent = 150;
                    reportViewer1.ZoomMode = ZoomMode.Percent;
                    break;
                case 4:
                    reportViewer1.ZoomPercent = 200;
                    reportViewer1.ZoomMode = ZoomMode.Percent;
                    break;
                case 5:
                    reportViewer1.ZoomPercent = 300;
                    reportViewer1.ZoomMode = ZoomMode.Percent;
                    break;
                case 6:
                    reportViewer1.ZoomPercent = 400;
                    reportViewer1.ZoomMode = ZoomMode.Percent;
                    break;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            leftTime--;
            if (leftTime == 0)
                this.Close();
            else
                skinLabel2.Text = leftTime.ToString();
        }
    }
}
